package com.husd.framework.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * 监视器
 * @author hushengdong
 */
public class MyZookeeperWatcher implements Watcher {

    ZooKeeper zooKeeper;

    public MyZookeeperWatcher() {
    }

    public MyZookeeperWatcher(ZooKeeper zooKeeper) {
        this.zooKeeper = zooKeeper;
    }

    public ZooKeeper getZooKeeper() {
        return zooKeeper;
    }

    public void setZooKeeper(ZooKeeper zooKeeper) {
        this.zooKeeper = zooKeeper;
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("eventType:" + watchedEvent.getType());
        if (watchedEvent.getType() == Event.EventType.NodeDataChanged) {
            try {
                Stat stat = new Stat();
                String path = watchedEvent.getPath();
                byte[] bytes = zooKeeper.getData(path, true, stat);
                System.out.println("path is : " + path + ".... new Data is : " + new String(bytes, "UTF-8"));
                //zooKeeper.exists(watchedEvent.getPath(), true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
